package com.example.app.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

/**
 * Created by 郭敏 on 2018/3/21 0021.
 */
public abstract class PreviewBaseLayout extends RelativeLayout {

    public PreviewBaseLayout(Context context) {
        super(context);
        init(context);
    }

    public PreviewBaseLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PreviewBaseLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    protected abstract void init(Context context);


    //true 禁止触摸
    private boolean isActivate;

    public void setActivateTouch(boolean isActivate) {
        this.isActivate = isActivate;
    }


    protected abstract void onTouch(MotionEvent event);

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isActivate) {
            return super.onTouchEvent(event);
        }
        onTouch(event);
        return true;
    }

    private AnimationSet animationSet;

    protected void onAnimationStop() {
        if (animationSet != null) {
            animationSet.cancel();
        }
    }

    protected void onFocusAnimator(View v) {
        if (animationSet != null) {
            animationSet.cancel();
        }
        v.setVisibility(View.VISIBLE);
        //创建AnimationSet 对象
        animationSet = new AnimationSet(true);
        //创建 ScaleAnimation 对象
        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //设置动画持续
        scaleAnimation.setDuration(1000);
        //动画插入器
        scaleAnimation.setInterpolator(getContext(), android.R.anim.decelerate_interpolator);
        //添加到AnimationSet
        animationSet.addAnimation(scaleAnimation);
        //animationSet.setFillAfter(true);
        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                onAnimatorStart();
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                onAnimatorEnd();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        v.startAnimation(animationSet);
    }

    protected void onAnimatorStart() {

    }

    protected void onAnimatorEnd() {

    }
}
